package org.sais.rasoid.deckeditor;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.sais.rasoid.CardViewActivity;
import org.sais.rasoid.R;
import org.sais.rasoid.Tools;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class DeckEditorActivity extends ListActivity implements OnItemSelectedListener {

	private static final String TAG = "Rasoid - DeckEditor";
	private static final int MESSAGE_DATABASE_READ = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cardlist);
		getListView().setFastScrollEnabled(true);
		new ReadCardDataBaseThread().start();

		Spinner spinner = (Spinner) findViewById(R.id.quick_access);
		// Create an ArrayAdapter using the string array and a default spinner
		// layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.quick_access_array,
				android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(this);
	}

	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MESSAGE_DATABASE_READ:
				ArrayList<String> values = new ArrayList<String>();
				values.add(getResources().getString(R.string.help));
				for (Integer i : Tools.getIndices()) {
					if (i < 0) {
						continue;
					}
					values.add(Tools.getCard(i).cardnum + " " + Tools.getCard(i).cardname);
				}
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
						R.layout.listitem_simplerow, values);
				getListView().setAdapter(adapter);
				break;
			}
		};
	};

	protected void onListItemClick(android.widget.ListView l, android.view.View v, int position, long id) {
		if (position == 0) {
			return;
		}
		Intent intent = new Intent(this, CardViewActivity.class);
		// Minus 1 due to the picture hint
		intent.putExtra("offset", position - 1);
		startActivity(intent);
	};

	class ReadCardDataBaseThread extends Thread {
		@Override
		public void run() {
			try {
				Tools.readCardBase(getApplicationContext());
				Tools.readPronunciation(getApplicationContext());
				mHandler.sendEmptyMessage(MESSAGE_DATABASE_READ);
			} catch (FileNotFoundException e) {
				Log.e(TAG, "", e);
			} catch (IOException e) {
				Log.e(TAG, "", e);
			}
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		int to;
		// Cowork
		if (pos == getResources().getStringArray(R.array.quick_access_array).length - 1) {
			to = Tools.getIndices().indexOf(9001);
		} else if (pos == 0) {
			to = 0;
		} else {
			to = Tools.getIndices().indexOf(pos * 100);
			if (to < 0) {
				to = Tools.getIndices().indexOf(pos * 100 + 1);
			}
		}
		getListView().setSelection(to + 1);
		// An item was selected. You can retrieve the selected item using
		// parent.getItemAtPosition(pos)
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// Another interface callback
	}

}
